package com.theaf.core.web;

import com.theaf.core.error.SmException;

import java.util.HashMap;
import java.util.Map;


/**
 * 处理返回结果
 * 
 * @author tone
 */

public class ResultHandler {

	/**
	 * 封装正常返回
	 * 
	 * @param obj
	 *            返回数据data部分
	 * @return
	 */

	public static Map<String,Object> success(Object obj) {
		Map<String,Object> map = new HashMap<>(2);
		map.put("status", 1000);
		map.put("data", obj);
		return map;
	}

	/**
	 * 封装异常返回处理
	 * 
	 * @param ex
	 * @return
	 */
	public static Map<String, Object> errorWrapper(Exception ex) {
		Map<String, Object> response = new HashMap<>(2);
		if (ex instanceof SmException) {
			SmException smEx = ((SmException) ex);
			if (SmException.SUCCESS_CODE == (int) smEx.getStatus()) {
				response.put("status", SmException.ERR_SYS_UNKNOWN);
			} else {
				response.put("status", smEx.getStatus());
			}
			response.put("message", smEx.getMessage());
		} else {
			response.put("status", SmException.ERR_SYS_UNKNOWN);
			response.put("message", "访问资源失败");
		}
		return response;
	}
}
